package com.ztej.query.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.ztej.query.R;
import com.ztej.query.model.Contact;
import com.ztej.query.parser.PullContactParser;
import com.ztej.query.utils.Tools;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class MainActivity extends Activity {

    private Button btnWebview, btnData, btnRequest, btnTree;

    List<Contact> contacts = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        btnWebview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
//                startActivity(intent);
            }
        });

        btnData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, DepartmentActivity.class);
                startActivity(intent);
            }
        });

        btnRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(){
                    public void run(){

                        try {
                            URL url = new URL("http://192.168.1.26:902/app/hr/Contacts.aspx?key=");
//                    String content = "";
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            conn.setConnectTimeout(5000);
                            conn.setReadTimeout(5000);
                            // 设置允许输出
                            conn.setDoOutput(true);
                            conn.setRequestMethod("POST");
                            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                            OutputStream os = conn.getOutputStream();
//                    os.write(content.getBytes("UTF-8"));
                            os.close();
                            //服务器返回的响应码
                            int code = conn.getResponseCode();
                            if (conn.getResponseCode() == 200){
                                InputStream is = conn.getInputStream();
//                                String text = Tools.getTextFromStream(is);
//                                text = text.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "").replace("<string>", "").replace("</string>","").replace("\r\n", "");
//                                System.out.println("---------------------------" + text);

                                contacts = new ArrayList<Contact>();
                                PullContactParser parser = new PullContactParser();
                                contacts = parser.parse(is);
                                for (Contact contact : contacts) {
                                    Log.i(TAG, contact.toString());
                                }


                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                }.start();


            }
        });

        btnTree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, TreeActivity.class);
                startActivity(intent);
            }
        });


    }

    private void initViews(){
        btnWebview = (Button)findViewById(R.id.btnWebview);
        btnData = (Button)findViewById(R.id.btnData);
        btnRequest = (Button)findViewById(R.id.btnRequest);
        btnTree = (Button)findViewById(R.id.btnTree);
    }



}
